/*请求url*/
var urlData = require("../../../../../data/url.js");

var falseData = {
    classArray:{
		code:0,
		classId:1,
		className:"美术课",
		teacher:"孙婉琳",
		timeStart:"123",
		classify:"艺术素养"
	},
    recordList:[
		{
			dynamic_id:999,
			dynamic_publisher:"sunwanlin999",
			dynamic_content:"",
			dynamic_image:[
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIka705f811d78348493bca931dd93967ab.jpg", 
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIk1cfbcd1a03be4769084a799b8a61b848.jpg", 
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIka1af6797a4df71972a94b43cc4265348.png", 
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIka705f811d78348493bca931dd93967ab.jpg",
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIk2476a1c034a7552368ede397ae3049f6.png"
			],
			dynamic_like:[
				{
					thumb_name:'sunwanlin1'
				},
				{
					thumb_name:'xiaoyuxin1'
				},
				{
					thumb_name:'chenyi1'
				},
				{
					thumb_name:'weihong1'
				},
				{
					thumb_name:'chensiyu1'
				}
			],
			dynamic_comment:[
				{
					comment_name:"sun1111",
					comment_content:"good111"
				},
				{
					comment_name:"sun1111",
					comment_content:"good1111"
				}
			]
		},
		{
			dynamic_id:222,
			dynamic_publisher:"sunwanlin222",
			dynamic_content:"",
			dynamic_image:[
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIka705f811d78348493bca931dd93967ab.jpg", 
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIk1cfbcd1a03be4769084a799b8a61b848.jpg", 
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIka1af6797a4df71972a94b43cc4265348.png", 
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIka705f811d78348493bca931dd93967ab.jpg",
                "wxfile://tmp_1387217230o6zAJs8axW1Jq5bfB7ydHPdLcsIk2476a1c034a7552368ede397ae3049f6.png"
			],
			dynamic_like:[
				{
					thumb_name:'sunwanlin12'
				},
				{
					thumb_name:'xiaoyuxin12'
				},
				{
					thumb_name:'chenyi12'
				},
				{
					thumb_name:'weihong12'
				},
				{
					thumb_name:'chensiyu12'
				}
			],
			dynamic_comment:[
				{
					comment_name:"sun1111222",
					comment_content:"good1112222"
				},
				{
					comment_name:"sun11112222",
					comment_content:"good11112222"
				}
			]
		}
	]
}
Page({
	data:{
		courseId:0,//从lits页带来的全局course_id
		userId:5,
		userRole:5,
		classArray:{},//课程数据
		listArray:[],//动态列表数据
		inputShow:false,
		/*url*/
    	classUrl: urlData.urlList.organManage.organRecord.detailClass.url,
    	listUrl:urlData.urlList.organManage.organRecord.detailList.url,
    	greatUrl:urlData.urlList.organManage.organRecord.detailGreat.url,
    	delGreatUrl:urlData.urlList.organManage.organRecord.detailDelGreat.url,
    	commentUrl:urlData.urlList.organManage.organRecord.detailComment.url,
    	delRecordUrl:urlData.urlList.organManage.organRecord.detailDelRecord.url,
	},
	/*
		监听页面从其他场景值回到当前场景
	*/
	onShow:function(e){
		this.onLoad();
	},
	/*
		页面加载
	*/
	onLoad:function(e){
		var that = this;
		//读出本地login数据缓存
		wx.getStorage({
		    key:"loginConf",
		    success: function(res) {
		    	console.log(res.data)
			    that.setData({
					userId : res.data.userId,
					userRole : res.data.userRole,
			    });  
			}
		});
		//读出本地course数据缓存
		wx.getStorage({
		    key:"organRecordListConf",
		    success: function(res) {
			    that.setData({
					courseId : res.data.courseId
			    });
			    that.sendCourseId();
			}
		});
	},
	/*
		两次发送课程id
	*/
	sendCourseId:function(e){
		var that = this;
		//发送当前课程id，请求detail页的class数据
		wx.request({
		    url: this.data.classUrl, 
		    data: {
		       course_id:this.data.courseId
		    },
		    header: {'content-type': 'application/json'},
		    success: function(res) {
		    	if(res.data.code == 0){
		    		that.setData({
		    			classArray : res.data
		    		});
		    	}
		    }
		});
		//发送当前课程id，请求detail页的list数据
		wx.request({
		    url: this.data.listUrl, //仅为示例，并非真实的接口地址
		    data: {
		       course_id:this.data.courseId,
		       user_id:this.data.userId,
		       role:this.data.userRole
		    },
		    header: {'content-type': 'application/json'},
		    success: function(res) {
		    	if(res.data.code == 0){
		    		that.setData({
		    			listArray : res.data.list
		    		});
		    	}
		    }
		});
	},
	/*
		添加动态
	*/
	add:function(e){
		wx.navigateTo({
	        url: '../organ-record-add/organ-record-add'
	    });
	},
	/*
		查看图片
	*/
	lookImg:function(e){		
		var recordIndex = e.currentTarget.dataset.recordindex,//当前动态的下标
	    	src = e.target.dataset.src,//当前图片路径
	    	imgs = this.data.listArray[recordIndex].dynamic_image;//所有图片的路径
	    if(src){
	    	wx.previewImage({
			    current: src,
			    urls: imgs
			});
	    }else{
	    	return false;
	    }
	},
	/*
		点赞
	*/
	great:function(e){
		var listArr = this.data.listArray;
		var that = this,
			recordIndex = e.currentTarget.dataset.recordindex,
			recordId = e.currentTarget.dataset.recordid;
		if(listArr[recordIndex].isThumb == 0){//可以点赞
			wx.request({
				url:this.data.greatUrl,
			    data: {
			       dynamic_id:recordId,
			       user_id:this.data.userId,
			       role:this.data.userRole
			    },
			    header: {'content-type': 'application/json'},
			    success: function(res) {
			    	if(res.data.code == 0){
			    		console.log(res.data.list)
						listArr[recordIndex].dynamic_like = res.data.list;
						listArr[recordIndex].isThumb = 1;
				    	that.setData({
							listArray : listArr
						});	
			    	}
			    }
			});
		}else{//已经点赞过了，不可以
			wx.request({
				url: this.data.delGreatUrl,
			    data: {
			       dynamic_id:recordId,
			       user_id:this.data.userId,
			       role:this.data.userRole
			    },
			    header: {'content-type': 'application/json'},
			    success: function(res) {
			    	if(res.data.code == 0){
			    		console.log(res.data.list)
			    		console.log('点赞已删除')
						listArr[recordIndex].dynamic_like = res.data.list;
						listArr[recordIndex].isThumb = 0;
				    	that.setData({
							listArray : listArr
						});	
			    	}
			    }
			});
		}
	},
	/*
		显示评论框
	*/
	inputShow:function(e){
		this.setData({
			inputShow : true
		});
	},
	/*
		添加评论
	*/
	commentAdd:function(e){
		var that = this;
		var text = e.detail.value;
		var recordIndex = e.currentTarget.dataset.recordindex;
		var listArr = this.data.listArray;
		console.log(e.currentTarget)
		wx.request({
			url: this.data.commentUrl,
		    data: {
		       dynamic_id:e.currentTarget.dataset.recordid,
		       user_id:this.data.userId,
		       role:this.data.userRole,
		       comment_content:text
		    },
		    header: {'content-type': 'application/json'},
		    success: function(res) {
		    	if(res.data.code == 0){
		    		listArr[recordIndex].dynamic_comment = res.data.list;
		    		that.setData({
		    			listArray : listArr,
		    			inputShow : false
		    		});
		    	}
		    }
		});
	},
	/*
		删除动态
	*/
	delRecord:function(e){
		var that = this;
		wx.showModal({
			title: '提示',
			content: '确定删除此条动态？',
			success: function(res) {
				if (res.confirm) {
					wx.request({
						url: that.data.delRecordUrl,
					    url: 'http://192.168.1.110:8000/communityDynamic/deletedynamic/', //仅为示例，并非真实的接口地址
					    data: {
					       dynamic_id:e.currentTarget.dataset.recordid
					    },
					    header: {'content-type': 'application/json'},
					    success: function(res) {
					    	if(res.data.code == 0){
					    		that.onLoad();
					    	}
					    }
					});
				} else if (res.cancel) {
					return false;
				}
			}
		})
		
	}
});